﻿Imports TBCC.UtilityMobile
Public Class CarHDStartUpService

    Public Shared Function GetHDStartUpCarList(ByVal projectId As String, ByVal startTime As DateTime, ByVal endTime As DateTime) As IList(Of ProjectInfo.CarHDStartUpItem)

        Dim carList As IList(Of ProjectInfo.CarHDStartUpItem)
        carList = New List(Of ProjectInfo.CarHDStartUpItem)
        Dim Hisendtime As DateTime


        Dim dt As DataSet
        Dim strsql As String = ""
        strsql += String.Format("select * from tbcchiststartup_{0}_1 where BeginTime between '{1}' and '{2}' order by BeginTime", projectId, startTime, endTime)

        'Select Case MainForm.readConfig("SystemType")
        '    Case 2
        '        dt = TBCC.UtilityMobile.SqlHelper1.ExecuteDataset(strsql, Nothing)
        '    Case Else
        dt = TBCC.UtilityMobile.SqlHelper.ExecuteDataset(strsql, Nothing)
        'End Select


        For Each r As DataRow In dt.Tables(0).Rows
            Dim car As New ProjectInfo.CarHDStartUpItem
            car.Id = SqlHelper.GetDbData(r("Id"))
            car.BeginTime = SqlHelper.GetDbData(r("BeginTime"))

            Hisendtime = SqlHelper.GetDbData(r("EndTime"))
            If Hisendtime = New DateTime(9999, 12, 31, 23, 59, 59) Then
                car.EndTime = "---------"
            Else
                car.EndTime = UtilityClass.GetDateString(Hisendtime)
            End If

            ' car.PageIndex = SqlHelper.GetDbData(r("PageIndex"))
            'car.PageCount = SqlHelper.GetDbData(r("PageCount"))
            car.RecordInterval = SqlHelper.GetDbData(r("RecordInterval"))
            'car.LastPageIndex = SqlHelper.GetDbData(r("LastPageIndex"))
            'car.LastPageType = SqlHelper.GetDbData(r("LastPageType"))
            car.BeginAddress = SqlHelper.GetDbData(r("BeginAddress"))
            car.EndAddress = SqlHelper.GetDbData(r("EndAddress"))
            car.Shipper = SqlHelper.GetDbData(r("Shipper"))
            car.Carrier = SqlHelper.GetDbData(r("Carrier"))
            car.Receiver = SqlHelper.GetDbData(r("Receiver"))
            'car.LastRecordTime = SqlHelper.GetDbData(r("LastRecordTime"))
            car.UpdateStatus = SqlHelper.GetDbData(r("UpdateStatus"))
            car.tlimitType = SqlHelper.GetDbData(r("tlimitType"))
            car.Tuplimit = SqlHelper.GetDbData(r("tuplimit"))
            car.Tdwlimit = SqlHelper.GetDbData(r("tdwlimit"))

            carList.Add(car)

        Next
        Return carList
    End Function
    Public Shared Function GetHDStartUpCarList(ByVal projectId As String) As IList(Of ProjectInfo.CarHDStartUpItem)
        Dim carList As IList(Of ProjectInfo.CarHDStartUpItem)
        carList = New List(Of ProjectInfo.CarHDStartUpItem)

        Dim dt As DataSet
        Dim strsql As String = ""
        strsql += String.Format("select * from tbcchiststartup_{0}_1 where finished=0 order by BeginTime", projectId)

        'Select Case MainForm.readConfig("SystemType")
        '    Case 2
        '        dt = TBCC.UtilityMobile.SqlHelper1.ExecuteDataset(strsql, Nothing)
        '    Case Else
        '        dt = TBCC.UtilityMobile.SqlHelper.ExecuteDataset(strsql, Nothing)
        'End Select
        dt = TBCC.UtilityMobile.SqlHelper.ExecuteDataset(strsql, Nothing)
        For Each r As DataRow In dt.Tables(0).Rows
            Dim car As New ProjectInfo.CarHDStartUpItem
            car.Id = SqlHelper.GetDbData(r("Id"))
            car.BeginTime = SqlHelper.GetDbData(r("BeginTime"))
            car.EndTime = SqlHelper.GetDbData(r("EndTime"))
            car.RecordInterval = SqlHelper.GetDbData(r("RecordInterval"))
            car.BeginAddress = SqlHelper.GetDbData(r("BeginAddress"))
            car.EndAddress = SqlHelper.GetDbData(r("EndAddress"))
            car.Shipper = SqlHelper.GetDbData(r("Shipper"))
            car.Carrier = SqlHelper.GetDbData(r("Carrier"))
            car.Receiver = SqlHelper.GetDbData(r("Receiver"))
            car.UpdateStatus = SqlHelper.GetDbData(r("UpdateStatus"))

            carList.Add(car)

        Next
        Return carList

    End Function

    Public Shared Function GetFDAPHDStartUpCarList(ByVal projectId As String) As IList(Of ProjectInfo.CarHDStartUpItem)
        Dim carList As IList(Of ProjectInfo.CarHDStartUpItem)
        carList = New List(Of ProjectInfo.CarHDStartUpItem)

        Dim dt As DataSet
        Dim strsql As String = ""
        strsql += String.Format("select  * from tbcchiststartup_{0}_1 where  fdapfinished={1} order by BeginTime limit 10", projectId, 0)

        'Select Case MainForm.readConfig("SystemType")
        '    Case 2
        '        dt = TBCC.UtilityMobile.SqlHelper1.ExecuteDataset(strsql, Nothing)
        '    Case Else
        '        dt = TBCC.UtilityMobile.SqlHelper.ExecuteDataset(strsql, Nothing)
        'End Select
        dt = TBCC.UtilityMobile.SqlHelper.ExecuteDataset(strsql, Nothing)
        For Each r As DataRow In dt.Tables(0).Rows
            Dim car As New ProjectInfo.CarHDStartUpItem
            car.Id = SqlHelper.GetDbData(r("Id"))
            car.BeginTime = SqlHelper.GetDbData(r("BeginTime"))
            car.EndTime = SqlHelper.GetDbData(r("EndTime"))
            car.RecordInterval = SqlHelper.GetDbData(r("RecordInterval"))
            car.BeginAddress = SqlHelper.GetDbData(r("BeginAddress"))
            car.EndAddress = SqlHelper.GetDbData(r("EndAddress"))
            car.Shipper = SqlHelper.GetDbData(r("Shipper"))
            car.Carrier = SqlHelper.GetDbData(r("Carrier"))
            car.Receiver = SqlHelper.GetDbData(r("Receiver"))
            car.UpdateStatus = SqlHelper.GetDbData(r("UpdateStatus"))

            carList.Add(car)

        Next
        Return carList

    End Function
    Public Shared Function GetFDAPHDStartCarList(ByVal projectId As String) As IList(Of ProjectInfo.CarHDStartUpItem)
        Dim carList As IList(Of ProjectInfo.CarHDStartUpItem)
        carList = New List(Of ProjectInfo.CarHDStartUpItem)

        Dim dt As DataSet
        Dim strsql As String = ""
        strsql += String.Format("select  * from tbcchiststartup_{0}_1 order by BeginTime limit 10", projectId)

        'Select Case MainForm.readConfig("SystemType")
        '    Case 2
        '        dt = TBCC.UtilityMobile.SqlHelper1.ExecuteDataset(strsql, Nothing)
        '    Case Else
        '        dt = TBCC.UtilityMobile.SqlHelper.ExecuteDataset(strsql, Nothing)
        'End Select
        dt = TBCC.UtilityMobile.SqlHelper.ExecuteDataset(strsql, Nothing)
        For Each r As DataRow In dt.Tables(0).Rows
            Dim car As New ProjectInfo.CarHDStartUpItem
            car.Id = SqlHelper.GetDbData(r("Id"))
            car.BeginTime = SqlHelper.GetDbData(r("BeginTime"))
            car.EndTime = SqlHelper.GetDbData(r("EndTime"))
            car.RecordInterval = SqlHelper.GetDbData(r("RecordInterval"))
            car.BeginAddress = SqlHelper.GetDbData(r("BeginAddress"))
            car.EndAddress = SqlHelper.GetDbData(r("EndAddress"))
            car.Shipper = SqlHelper.GetDbData(r("Shipper"))
            car.Carrier = SqlHelper.GetDbData(r("Carrier"))
            car.Receiver = SqlHelper.GetDbData(r("Receiver"))
            car.UpdateStatus = SqlHelper.GetDbData(r("UpdateStatus"))

            carList.Add(car)

        Next
        Return carList

    End Function
    Public Shared Function GetMaxHDStartUpCar(ByVal projectId As String) As Integer

        Dim strSql As String = ""
        strSql += String.Format("select max(id) from tbcchiststartup_{0}_1  order by BeginTime", projectId)
        Dim obj As Object
        'Select Case MainForm.readConfig("SystemType")
        '    Case 2
        '        obj = SqlHelper1.ExecuteScalar(strSql, Nothing)
        '    Case Else
        obj = SqlHelper.ExecuteScalar(strSql, Nothing)
        'End Select


        If obj Is Nothing OrElse IsDBNull(obj) Then
            Return -1
        End If

        Return CInt(obj)

    End Function

    Public Shared Function SetStartStopDB(ByVal pi As String, ByVal carstartstop() As CarWebServiceAdd.StartUpView) As Boolean
        Dim strSql As String
        Dim tic As New TBCC.UtilityMobile.TranstrationItem

        strSql = String.Format("Insert Into tbcchiststartup_{0}_1(id,BeginTime,EndTime,RecordInterval,BeginAddress,EndAddress,Shipper,Carrier,Receiver,UpdateStatus,Finished,tlimitType,sent,tuplimit,tdwlimit,Fdapfinished) Values(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7,?p8,?p9,?p10,?p11,0,?p12,?p13,?p14);-- ", pi)
        For k As Integer = 0 To carstartstop.Length - 1
            Dim CarStartStopClass As New CarWebServiceAdd.StartUpView
            CarStartStopClass = carstartstop(k)
            Dim p(14) As MySql.Data.MySqlClient.MySqlParameter
            For i As Integer = 0 To p.GetUpperBound(0)
                p(i) = New MySql.Data.MySqlClient.MySqlParameter
                p(i).ParameterName = "p" + i.ToString
            Next
            p(0).Value = CarStartStopClass.id
            p(1).Value = CarStartStopClass.beginTime
            p(2).Value = CarStartStopClass.endTime
            p(3).Value = CarStartStopClass.recordInterval
            p(4).Value = CarStartStopClass.beginAddress
            p(5).Value = CarStartStopClass.endAddress
            p(6).Value = CarStartStopClass.shipper
            p(7).Value = CarStartStopClass.carrier
            p(8).Value = CarStartStopClass.receiver
            p(9).Value = CarStartStopClass.updateStatus
            p(10).Value = 0
            p(11).Value = CarStartStopClass.tlimitType
            p(12).Value = CarStartStopClass.tuplimit
            p(13).Value = CarStartStopClass.tdwlimit
            p(14).Value = 0

            tic.Add(strSql & k.ToString, p)
        Next

        'Select Case MainForm.readConfig("SystemType")
        '    Case 2
        '        Return SqlHelper1.ExecuteTranstration(tic)
        '    Case Else
        Return SqlHelper.ExecuteTranstration(tic)
        'End Select

    End Function
    Public Shared Function GetStartStopSpace(ByVal pi As String, ByVal id As Integer) As Integer

        Dim strSql As String = ""
        strSql += String.Format("select RecordInterval from tbcchiststartup_{0}_1 where id={1} order by BeginTime", pi, id)
        Dim obj As Object
        'Select Case MainForm.readConfig("SystemType")
        '    Case 2
        '        obj = SqlHelper1.ExecuteScalar(strSql, Nothing)
        '    Case Else
        obj = SqlHelper.ExecuteScalar(strSql, Nothing)
        'End Select

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Return -1
        End If

        Return CInt(obj)

    End Function
    Public Shared Function UpdateFDAPPort(ByVal pi As String, ByVal StartStopid As Integer) As Boolean

        Dim strSql As String
        Dim tic As New TBCC.UtilityMobile.TranstrationItem
        strSql = String.Format("update  tbcchiststartup_{0}_1 set Fdapfinished=?p0 where id=?p1", pi)

        Dim p(1) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.GetUpperBound(0)
            p(i) = New MySql.Data.MySqlClient.MySqlParameter
            p(i).ParameterName = "p" + i.ToString
        Next
        p(0).Value = 1
        p(1).Value = StartStopid


        tic.Add(strSql, p)

        'Select Case MainForm.readConfig("SystemType")
        '    Case 2
        '        Return SqlHelper1.ExecuteTranstration(tic)
        '    Case Else
        Return SqlHelper.ExecuteTranstration(tic)
        'End Select

    End Function
    Public Shared Function UpdateFDAPLastPort(ByVal pi As String, ByVal StartStopid As Integer) As Boolean

        Dim strSql As String
        Dim tic As New TBCC.UtilityMobile.TranstrationItem
        strSql = String.Format("update  tbcchiststartup_{0}_1 set Fdapfinished=?p0 where id=?p1", pi)

        Dim p(1) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.GetUpperBound(0)
            p(i) = New MySql.Data.MySqlClient.MySqlParameter
            p(i).ParameterName = "p" + i.ToString
        Next
        p(0).Value = 2
        p(1).Value = StartStopid


        tic.Add(strSql, p)

        'Select Case MainForm.readConfig("SystemType")
        '    Case 2
        '        Return SqlHelper1.ExecuteTranstration(tic)
        '    Case Else
        Return SqlHelper.ExecuteTranstration(tic)
        'End Select

    End Function
    Public Shared Function UpdateStartStopDB(ByVal pi As String, ByVal StartStopid As Integer, ByVal carHD() As CarWebServiceAdd.CarHisView_new) As Boolean
        Dim strSql As String
        Dim tic As New TBCC.UtilityMobile.TranstrationItem
        strSql = String.Format("update  tbcchiststartup_{0}_1 set endtime=?p0,Finished=?p1,UpdateStatus=?p3,Fdapfinished=?p4 where id=?p2", pi)

        Dim p(4) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.GetUpperBound(0)
            p(i) = New MySql.Data.MySqlClient.MySqlParameter
            p(i).ParameterName = "p" + i.ToString
        Next
        p(0).Value = carHD(0).endTime
        p(1).Value = 1
        p(2).Value = StartStopid
        p(3).Value = 2
        p(4).Value = 0


        tic.Add(strSql, p)

        'Select Case MainForm.readConfig("SystemType")
        '    Case 2
        '        Return SqlHelper1.ExecuteTranstration(tic)
        '    Case Else
        Return SqlHelper.ExecuteTranstration(tic)
        'End Select
    End Function
End Class
